package com.xiaode.controller.app.task.exercise;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.jfinal.aop.Before;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xiaode.common.constant.Const;
import com.xiaode.common.model.ExerciseTask;
import com.xiaode.exce.EnumError;
import com.xiaode.util.UserUtil;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

public class ExerciseService {

	/**
     * 锻炼打卡
     */
    @Before(Tx.class)
    public Ret exerciseClock(Integer accountId, Integer userId, Date date) throws Exception {
        //查询打卡时间 是否已经打过卡
        LocalDateTime startDateTime = DateUtil.beginOfDay(date).toLocalDateTime();
        LocalDateTime endDateTime = DateUtil.endOfDay(date).toLocalDateTime();
        ExerciseTask exerciseRecord = ExerciseTask.dao.findFirst("SELECT * FROM exercise_task WHERE user_account_id = ? AND date between ? AND ?", accountId, startDateTime, endDateTime);
        if (BeanUtil.isNotEmpty(exerciseRecord)) {
            return Ret.fail().set("error", EnumError.EXERCISE_HAVE_REPEAT.getObj());
        }
        //查询是否是今日
        boolean sameDay = DateUtil.isSameDay(date, new Date());
        ExerciseTask record = new ExerciseTask();
        record.setUserAccountId(accountId);
        LocalDateTime localDateTime = DateUtil.toLocalDateTime(date);
        record.setDate(localDateTime);
        //如果是当天打卡 则加分
        int scoreChange = 0;
        if (sameDay) {
        	scoreChange = 1;
        	UserUtil.addScoreForStu(userId, scoreChange, Const.addMarkEventType.TODAY_EXERCISE, null);
        } else {
            record.setIsRepair(true);
        }
        record.save();
        
        return Ret.ok("exerciseId", record.getId()).set("scoreChange", scoreChange);
    }

    /**
     * 更新打卡
     */
    public Ret updateExercise(Integer accountId, Integer exerciseId, String sports, String content) throws Exception {

        ExerciseTask record = ExerciseTask.dao.findById(exerciseId);
        if (BeanUtil.isEmpty(record)) {
            return Ret.fail().set("error", EnumError.EXERCISE_NOT_EXIST.getObj());
        }
        if (!record.getUserAccountId().equals(accountId)) {
            return Ret.fail().set("error", EnumError.EXERCISE_NOT_BELONE_TO_YOU.getObj());
        }
        record.setContent(content);
        record.setSports(sports);
        record.update();
        return Ret.ok();
    }

    /**
     * 查询打卡记录
     */
    public Ret findExercise(Integer accountId, Integer accountIdMy, Date date) throws Exception {
    	accountIdMy = accountId == null ? accountIdMy : accountId;
        //获取查询日期的当月锻炼打卡记录
        LocalDateTime startTime = DateUtil.beginOfMonth(date).toLocalDateTime();
        LocalDateTime endTime = DateUtil.endOfMonth(date).toLocalDateTime();
        List<ExerciseTask> exerciseRecords = ExerciseTask.dao.find("SELECT * FROM exercise_task WHERE user_account_id = ? AND date BETWEEN ? AND ?", accountIdMy, startTime, endTime);
        return Ret.ok("exerciseRecord", exerciseRecords);
    }
    

}
